package welsir.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import welsir.UserTokenGenerate;
import welsir.common.FlowerPool;
import welsir.entity.dos.User;
import welsir.entity.dos.UserRole;
import welsir.entity.enums.ResultCode;
import welsir.entity.vo.UserVO;
import welsir.exception.ServiceException;
import welsir.mapper.UserMapper;
import welsir.mapper.UserRoleMapper;
import welsir.security.Token;
import welsir.service.IUserService;
import welsir.util.MathUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author welsir
 * @date 2023/3/3 20:44
 */
@Service
@Slf4j
public class IUserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserTokenGenerate userTokenGenerate;

    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 通过用户名获取用户
     *
     * @param username 用户名
     * @return 会员信息
     */
    public User findByUsername(String username) {
        return queryUserByUserName(username);
    }

    /**
     * 登录：用户名、密码登录
     *
     * @param username 用户名
     * @param password 密码
     * @return token
     */
    public Map usernameLogin(String username, String password) {
        User user = this.findByUsername(username);
//        如果用户不存在或者已经被删除
        if (user == null) {
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        if (!password.equals(user.getPassword())) {
            throw new ServiceException(ResultCode.USER_PASSWORD_ERROR);
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
//        登录成功，创建token并返回
        return Map.of("userInfo",userVO,"token",userTokenGenerate.createToken(user, false).getAccessToken());
    }

    @Override
    public User queryUserByUserName(String userName) {
        return  query().eq("username",userName).one();
    }

    @Override
    public User queryUserByUid(Long uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public List<User> queryAllUser() {
        log.info(String.valueOf(userMapper.selectList(null)));
        return userMapper.selectList(null);
    }

    @Override
    public int deleteUserByUid(Long uid) {
        return userMapper.deleteById(uid);
    }

    @Override
    public int updateUserByUser(User user) {
        System.out.println(user);
        return userMapper.updateById(user);
    }

    @Override
    public Map registerUser(User user) {
        if (this.queryUserByUserName(user.getUsername())!=null) {
            throw new ServiceException(ResultCode.ERROR);
        }
        int uid = MathUtil.randomDigitNumber(9);
        user.setUid(uid);
        userMapper.insert(user);
        userRoleMapper.insert(new UserRole(uid, FlowerPool.SHIRO.USER.getVal()));
        return this.usernameLogin(user.getUsername(), user.getPassword());

    }

    @Override
    public Page<User> queryUserPage(int size, int page) {
        Page<User> userPage = new Page(page,size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        return this.baseMapper.selectPage(userPage, queryWrapper);
    }
}
